package cn.tedu.boot03.controller;

import cn.tedu.boot03.entity.Product;
import cn.tedu.boot03.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//使用RestController相当于在每一个方法的上面都添加了@ResponseBody注解
@RestController
public class ProductController {
    /*自动装配 此框架添加后,Mybatis和Spring 框架会生成ProductMapper
    * 的实现类,并且实例化该实现类(实现类里面会实现ProductMapper中的抽象方法,
    * 实现的方法里面写的就是JDBC代码),并且把实例化好的对象赋值给了mapper变量*/
    @Autowired
    ProductMapper mapper;
    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product = " + product);
        mapper.insert(product);
        return "添加完成!<a href='/'>返回首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        List<Product> list = mapper.select();
        String html = "<table border='2'>";
        html += "<caption>商品列表</caption>";
        html += "<tr><th>id</th><th>商品标题</th><th>商品价格</th><th>商品库存</th><th>操作</th>";
        for(Product p:list){
            html += "<tr>";
            html += "<td>"+p.getId()+"</td>";
            html += "<td>"+p.getTitle()+"</td>";
            html += "<td>"+p.getPrice()+"</td>";
            html += "<td>"+p.getNum()+"</td>";
            html += "<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";/*?获取参数*/
            html += "</tr>";
        }
        html += "</table>";
        return html;

    }
    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = " + id);
        mapper.deleteBy(id);
        return "删除完成!<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Product product){
        System.out.println("product = " + product);
        mapper.update(product);
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }

}
